﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SwParaPlatform2.dlgRuleConfig
{

    public partial class dlgNodeSelectTree : Form
    {
        public event EventHandler<string> NodeSelected;
        private string selectedNodeName;
        private Dictionary<string, Dictionary<string, object>> data;
        private Dictionary<string, object> selectedNodeData;

        public dlgNodeSelectTree(Dictionary<string, Dictionary<string, object>> data)
        {
            this.data = data;
            InitializeComponent();
            InitializeTreeView();
            LoadTreeData(data);
        }

        private void InitializeTreeView()
        {
            this.treeView.AfterSelect += TreeView_AfterSelect;
            this.treeView.NodeMouseDoubleClick += TreeView_NodeMouseDoubleClick;
        }

        private void LoadTreeData(Dictionary<string, Dictionary<string, object>> data)
        {
            foreach (var mainNode in data)
            {
                var mainTreeNode = new TreeNode("展开查看所有模型文件") { Tag = mainNode.Value };
                if (mainNode.Value.ContainsKey("SubComp"))
                {
                    var subComponents = mainNode.Value["SubComp"] as Dictionary<string, object>;
                    foreach (var subNode in subComponents)
                    {
                        var subTreeNodeData = subNode.Value as Dictionary<string, object>;
                        var subTreeNode = new TreeNode(subNode.Key) { Tag = subTreeNodeData };

                        // Check if this node is of type "Component"
                        if (subTreeNodeData != null && subTreeNodeData.ContainsKey("TypeName") && subTreeNodeData["TypeName"].ToString() == "Component")
                        {
                            subTreeNode.ForeColor = System.Drawing.Color.Black; // 可选子节点
                        }
                        else
                        {
                            subTreeNode.ForeColor = System.Drawing.Color.Gray; // 非可选子节点
                        }

                        mainTreeNode.Nodes.Add(subTreeNode);
                    }
                    mainTreeNode.Expand();
                }
                this.treeView.Nodes.Add(mainTreeNode);
            }
        }

        private void TreeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Tag is Dictionary<string, object> nodeData &&
                nodeData.ContainsKey("TypeName") && nodeData["TypeName"].ToString() == "Component")
            {
                selectedNodeName = e.Node.Text;
                selectedNodeData = nodeData;
            }
            else
            {
                selectedNodeName = null;
                selectedNodeData = null;
            }
        }

        private void TreeView_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Node.Tag is Dictionary<string, object> nodeData &&
                nodeData.ContainsKey("TypeName") && nodeData["TypeName"].ToString() == "Component")
            {
                NodeSelected?.Invoke(this, selectedNodeName);
                this.Close();
            }
        }

        private void selectButton_Click_1(object sender, EventArgs e)
        {
            if (selectedNodeData != null)
            {
                NodeSelected?.Invoke(this, selectedNodeName);
                this.Close();
            }
        }
    }

    public class NodeSelectedEventArgs : EventArgs
    {
        public Dictionary<string, object> SelectedData { get; }

        public NodeSelectedEventArgs(Dictionary<string, object> selectedData)
        {
            SelectedData = selectedData;
        }
    }
}
